// ==UserScript==
// @name        userScriptBasedDojo
// @namespace   bob
// @description userScriptBasedDojo
// @include     *
// @version     1
// ==/UserScript==
function inject(content, type, id) {
	var s = null;
	if (type == 'css') {
		s = document.createElement("style");
		s.type = "text/css";
	} else {
		s = document.createElement("script");
		s.type = "text/javascript";
	}
	if (id)
		s.id = "gandc";
	s.textContent = content;
	(type=="css"?document.head:document.body).appendChild(s);
}
function addSrcScript(src,callback,arg_str) {
  var script = document.createElement("script");
  script.setAttribute("src", src);
  script.addEventListener('load', function() {
    textContent = "(" + callback.toString() + ")("+(arg_str||"")+")";
    inject(textContent);
  }, false);
  document.body.appendChild(script);
}
function loadSrcCss(src,id){
     var cssTag = document.getElementById(id);
     var head = document.getElementsByTagName('head').item(0);
     if(cssTag) head.removeChild(cssTag);
     css = document.createElement('link');
     css.href = src;
     css.rel = 'stylesheet';
     css.type = 'text/css';
     if(id){css.id = id;}
     head.appendChild(css);
}
// function globalCallBackWithJquery($){
// 	console.log("执行用户脚本");
// 	console.log($.fn.jquery);
// 	console.log(baidu);
// }

// var src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js";
// if(typeof jQuery=="undefined"){
// 	addSrcScript(src,globalCallBackWithJquery,"jQuery");
// }else{
// 	globalCallBackWithJquery(jQuery);
// }

function globalCallBackWithDojo(dj){
	//这里做你加载完js后的操作
	console.log("已经载入dojo，开始执行用户自定义脚本,"+location.href);
	console.log(dj);
	console.log(dojo);
	dojo.query("body").addClass("claro");
	// require(["dojo/date/locale", "dijit/Dialog","dojo/_base/window", "dojo/i18n", "dojo/domReady!"]
	// 		, function(locale, Dialog,win) {
	// 			dojo.query("body").addClass("claro");
	// 			var now = new Date();
	// 			var dlg = new Dialog({
	// 				id: 'dialog',
	// 				title: 'Today: '+ locale.format(now, {
	// 						formatLength:'full',selector:'date'
	// 				})
	// 			}).placeAt(win.body());
	// 			dlg.startup();
				
	// 			dlg.set("content", '<h1>Bob Shi，你丫真牛逼！</h1>');
	// 			dlg.show();
	// });
// require(["dojo/ready"], function(ready){
//     ready(function(){
    	var funcList = [{id:"about",label:"关于",onClick:about}];
    	if(typeof window.pp_play!='undefined'){
    		funcList.push({id:"showBdMovieList",label:"获取影片链接",onClick:showBdMovieList});
    	}
        showPane(funcList);
//     });
// });

function showPane(funcList){
	require(["dijit/TitlePane","dijit/Menu","dijit/MenuItem","dojo/NodeList-manipulate"],function(TitlePane,Menu,MenuItem){
		var isOpen = (!funcList)||funcList.open||(funcList.length==0);
		var funcPane = new dijit.TitlePane({id:"funcPane",title:"功能列表(针对本页)",open:isOpen,style:{position:"fixed",right:0,bottom:0}});
        dojo.query("body").append(funcPane.domNode);
        dojo.style(funcPane.containerNode,{padding:"0"});
        var menuContainer = dojo.create("div",{id:"menuContainer"},funcPane.containerNode);
        var menu = new Menu({style:{width:"100%",height:"100%"}},'menuContainer');
        dojo.forEach(funcList,function(func){
        	menu.addChild(new MenuItem({
	            id: func.id,
	            label: func.label,
	            onClick:func.onClick
       	 	}) );
        });
        

         menu.startup();
	});
	
}

function about(evt){
	require(["dijit/Dialog"], function(Dialog){
        aboutDialog = new Dialog({
            title: "关于页面用户脚本",
            content: "Powered by Bob Shi",
            style: "width: 150px"
        });
        aboutDialog.show();
	});
}

function showBdMovieList(){ 
	// var bdUrlListStr=bdUrlList.join("\n");
	// alert(bdUrlListStr);
	require(["dijit/Dialog"], function(Dialog){
		var pp_url=window.pp_play;
		  if(typeof pp_url=='undefined'||!pp_url){alert('不是在奇热网的剧集播放页面,不能获取播放(下载)URL!');return;};
		  var ppList= decodeURIComponent(pp_url).split("$$$");
		  var bdIndex = -1;
		  for(var j=0;j<ppList.length;j++){
		    if(ppList[j].indexOf("bdhd://")>-1){bdIndex=j;break;}
		  }
		  if(bdIndex<0){alert("没有找到可用百度影音下载链接");return;}
		  bdList = ppList[bdIndex].split("+++");
		  // alert("共找到"+bdList.length+"个影片");
		  var bdUrlList=[];
		  for(var i=0;i<bdList.length;i++){
		    bdUrlList.push(bdList[i].split("++")[1]);
		  }
	    var showUrlDialog = new Dialog({
	    	id:"showUrlDialog",
	        title: "共找到"+bdUrlList.length+"个影片",
	        content:bdUrlList.join("<br/>")
	    });
	   console.log(dojo.query(showUrlDialog.containerNode).position());
	   // dojo.query(txtArea)
	   showUrlDialog.show();
	   // var dlgContainerPosition = dojo.query(showUrlDialog.containerNode).position()[0];
	   dojo.query(showUrlDialog.containerNode).style("width");
	   var w = dojo.query(showUrlDialog.containerNode).style("width");
	   // dojo.query(showUrlDialog.containerNode).style("width",(width+20)+"px");
	   var h = dojo.query(showUrlDialog.containerNode).style("height");
	   window.bdUrlList = bdUrlList;
	   var txtArea = dojo.create("textarea",{value:bdUrlList.join("\n")},showUrlDialog.containerNode,"only");

	   dojo.style(txtArea,{width:w+"px",height:h+"px"});
		txtArea.focus();
		txtArea.select();
	});
}

}

//================================================
var msg = "";
var isRun = true;
if (top.location != self.location){
	msg = "iframe中不执行dojo自定义脚本:"+location.href;
	isRun = false;
}else if(location.host=="dojotoolkit.org"){
	msg = "dojo官网不执行"+location.href;
	isRun = false;
}
if(!isRun){
	console.log(msg);
	return;
}
var debug = 1;
if(debug){
	var baseUrl = "http://lscn817.51vip.biz:8080/ajax/dojo-1.8.3";
	var src = baseUrl+"/dojo/dojo.js.uncompressed.js";//你要引入的js文件
}else{
	var baseUrl = "http://ajax.googleapis.com/ajax/libs/dojo/1.8.2";
	var src = baseUrl+"/dojo/dojo.js";//你要引入的js文件
}
if(typeof dojo=="undefined"){//js文件是否正常引入判断
	var dojoConfigStr = "var dojoConfig = {async:0,blankGif:'"+baseUrl+"/dojo/resources/blank.gif'};"
	inject(dojoConfigStr);
	// loadSrcCss("http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dijit/themes/claro/claro.css");//你要引入的css文件
	loadSrcCss(baseUrl+"/dijit/themes/claro/claro.css");//你要引入的css文件
	addSrcScript(src,globalCallBackWithDojo,"dojo");
}else{
	globalCallBackWithDojo(dojo);
}